﻿KISSY.use("calendar", function (S, Calendar) {
    S.ready(function (S) {
        var All = S.all,
            currentDate = new Date(),
            year = currentDate.getFullYear(),
            month = currentDate.getMonth(),
            day = currentDate.getDate(),
            minDate = currentDate,
            maxDate = new Date(year + 10, month, day),


            startDateInput = All("#J_StartDate"),
            startDateInputText = startDateInput.val(),
            startDateCalendar,
            defaultStartDate = currentDate,

            endDateInput = All("#J_EndDate"),
            endDateInputText = endDateInput.val(),
            endDateCalendar,
            defaultEndDate = currentDate;

        if (S.isDate(startDateInputText)) {
            defaultStartDate = new Date(startDateInputText);
        }
        if (S.isDate(endDateInputText)) {
            defaultEndDate = new Date(endDateInputText);
        }

        startDateCalendar = new Calendar(startDateInput, {
            date: defaultStartDate,
            selected: defaultStartDate,
            minDate: minDate,
            maxDate: maxDate,
            popup: true,
            closable: true,
            align: { points: ['bl', 'tl'], offset: [0, 0] }
        });

        endDateCalendar = new Calendar(endDateInput, {
            date: defaultEndDate,
            selected: defaultEndDate,
            minDate: minDate,
            maxDate: maxDate,
            popup: true,
            closable: true,
            align: { points: ['bl', 'tl'], offset: [0, 0] }
        });

        startDateCalendar.on("select", function (event) {
            startDateInput.val(S.Date.format(event.date, "isoDate"));
        });

        endDateCalendar.on("select", function (event) {
            endDateInput.val(S.Date.format(event.date, "isoDate"));
        });


    });
});